from peewee import *

db = MySQLDatabase('test',
                   **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'user': 'root',
                      'password': 'root'})


class UnknownField(object):
    def __init__(self, *_, **__): pass


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    age = IntegerField(null=True)
    birthday = DateTimeField(null=True)
    name = CharField(null=True)

    class Meta:
        table_name = 'user'


# 批量提交，自动计算事务
from random import randint

data = [
    {'name': f'周八{i}号', 'age': randint(20, 40), 'birthday': f'{randint(1980, 2010)}-{randint(1, 13)}-{randint(1, 29)}'}
    for i in range(10000)
]

# Assume there are 789 items in row_data. The following code will result in
# 8 total transactions (7x100 rows + 1x89 rows).
for row in db.batch_commit(data, 100):
    User.create(**row)
